﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;

namespace Gui.MyUserControl
{
    /// <summary>
    /// Pager.xaml 的交互逻辑
    /// </summary>
    public partial class Pager : UserControl, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        public List<PageSizeItem> PageSizeItems
        {
            get {
                return new List<PageSizeItem>
                {
                    new PageSizeItem(10,"10条/页"),
                    new PageSizeItem(20,"20条/页"),
                    new PageSizeItem(50,"50条/页"),
                    new PageSizeItem(100,"100条/页")
                };
            }
        }

        public static readonly DependencyProperty TotalPageProperty =
           DependencyProperty.Register("TotalPage", typeof(int), typeof(Pager), new PropertyMetadata(1, (d, e) => {
               var pager = d as Pager;
               pager.RaisePropertyChanged("PrevEnable");
               pager.RaisePropertyChanged("NextEnable");
           }));

        public static readonly DependencyProperty CurrentPageProperty = 
            DependencyProperty.Register("CurrentPage", typeof(int), typeof(Pager), new PropertyMetadata(1,(d,e)=> {
                var pager = d as Pager;
                pager.RaisePropertyChanged("PrevEnable");
                pager.RaisePropertyChanged("NextEnable");
            }));

        public static readonly RoutedEvent pageChangeEvent =
            EventManager.RegisterRoutedEvent("PageChange", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(Pager));

        public Pager()
        {
            InitializeComponent();
            combo.SelectedIndex = 0;
        }


        public int TotalPage
        {
            get 
            { 
                return (int)GetValue(TotalPageProperty);
            }
            set
            {
                SetValue(TotalPageProperty, value);
            }
        }

        public int CurrentPage
        {
            get 
            { 
                return (int)GetValue(CurrentPageProperty);
            }
            set
            {
                SetValue(CurrentPageProperty, value);
            }
        }
       
        public bool PrevEnable
        {
            get
            {
                return CurrentPage != 1;
            }
        }

        public bool NextEnable
        {
            get
            {
                return CurrentPage != TotalPage;
            }
        }

        public event RoutedEventHandler PageChange
        {
            add
            {
                AddHandler(pageChangeEvent, value);
            }

            remove
            {
                RemoveHandler(pageChangeEvent, value);
            }
        }

        private void FirstClick(object sender, RoutedEventArgs e)
        {
            PageChangeArgs args = new PageChangeArgs(pageChangeEvent, this, 1, GetPageSize());
            RaiseEvent(args);
        }

        private void LastClick(object sender, RoutedEventArgs e)
        {
            PageChangeArgs args = new PageChangeArgs(pageChangeEvent, this, TotalPage, GetPageSize());
            RaiseEvent(args);
        }

        private void PrevClick(object sender, RoutedEventArgs e)
        {
            PageChangeArgs args = new PageChangeArgs(pageChangeEvent, this, CurrentPage - 1, GetPageSize());
            RaiseEvent(args);
        }

        private void NextClick(object sender, RoutedEventArgs e)
        {
            PageChangeArgs args = new PageChangeArgs(pageChangeEvent, this, CurrentPage + 1, GetPageSize());
            RaiseEvent(args);
        }
       
        private int GetPageSize()
        {
            return ((PageSizeItem)combo.SelectedItem).Size;
        }

        public void RaisePropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        private void combo_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            PageChangeArgs args = new PageChangeArgs(pageChangeEvent, this, CurrentPage, GetPageSize());
            RaiseEvent(args);
        }
    }

    public class PageChangeArgs : RoutedEventArgs
    {
        public PageChangeArgs(RoutedEvent routedEvent, object source, int pageNo, int pageSize) : base(routedEvent, source) {
            PageNo = pageNo;
            PageSize = pageSize;
        }
        public int PageNo { get; set; }
        public int PageSize { get; set; }
    }

    public class PageSizeItem
    {
        internal PageSizeItem(int size, string content) {
            this.Size = size;
            this.Content = content;
        }
        public int Size { get; set; }
        public string Content { get; set; }
    }

}